//
//  ViewController.m
//  checkApp
//
//  Created by ma on 2017/6/12.
//  Copyright © 2017年 ma. All rights reserved.
//

#import "ViewController.h"
#import "TabBarViewController.h"

@interface ViewController ()

@end

@implementation ViewController
{
    UILabel *label;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    double delayInSeconds = 1;
    dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
    dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
        
    });
    
    [self initView];
}

- (void)initView {
    
    UIImageView *topImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"error"]];
    topImage.frame = CGRectMake((screen_width-300)/2+15*boringScale, 30, 300, 300);
    topImage.center = CGPointMake(screen_width/2+7.5*boringScale, screen_height/2-20*boringScale);
    [self.view addSubview:topImage];
    
    UIButton *button = [UIButton buttonWithFrame:CGRectMake(screen_width-50*boringScale, 30, 25*boringScale, 25*boringScale) title:nil image:@"refresh" target:self action:@selector(refreshBtnClick:)];
    [self.view addSubview:button];
    
    label = [UILabel labelWithFrame:CGRectMake(50, topImage.bottom+30, screen_width-100, 30) text:self.result?@"请找Loger添加权限":@"网络有误，刷新重试" font:15.0*boringScale textColor:red_color];
    label.textAlignment = 1;
    [self.view addSubview:label];
}

- (void)refreshBtnClick:(UIButton *)button {
    NSString *identifierForVendor = [[UIDevice currentDevice].identifierForVendor UUIDString];
    NSDictionary *params = @{@"machineCode":@"loger168",
                             @"projectType":@"1"};
    button.enabled = NO;
    [NetRequest postDataWithUrlString:ITEM_LIST_URL withParams:params success:^(id data) {
        NSLog(@"data is %@",data);//无效的机器码
        if ([data[@"status"] integerValue] == 1008) {
            [self showErrorWithTitle: @"无操作权限" autoCloseTime: 2];
            label.text = @"请找Loger添加权限";
            button.enabled = YES;
        }else {
            double delayInSeconds = 2;
            dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
            dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
                AppDelegateInstance.window.rootViewController = [[TabBarViewController alloc] init];
            });
            
        }
    } fail:^(NSString *errorDes) {
        NSLog(@"数据请求失败");
        label.text = @"网络有误，刷新重试";
        button.enabled = YES;
        [self showErrorWithTitle: @"网络有误" autoCloseTime: 2];
//        AppDelegateInstance.window.rootViewController = [[TabBarViewController alloc] init];
    } refresh:^{
        [self refreshBtnClick:button];
    }];
}

- (void)viewDidAppear:(BOOL)animated {
//    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:self.result?@"此设备无操作权限":@"网络有误" preferredStyle:UIAlertControllerStyleAlert];
//    [alertController addAction:[UIAlertAction actionWithTitle:@"知道了" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
//        [self initView];
//    }]];
//    [self presentViewController:alertController animated:YES completion:nil];
//    [self showErrorWithTitle: @"无操作权限" autoCloseTime: 2];
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
